package com.woniuxy.oa.controller; 

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

// HTML文件经过模板引擎统一处理 
@Controller
public class StaticController {
public static final String[] rule1 = { "content", "index", "menu", "password", "top" };
	
	private int checkUrl(String newUrl) {
		int flag = 0;
		if ("login".equals(newUrl)) {
			// 情况1
			flag = 1;
			return flag;
		}else {
			flag = 3;
		}
		for (int i = 0; i < rule1.length; i++) {
			String str = rule1[i];
			if (str.equals(newUrl)) {
				flag = 2;
			} 
		}
		return flag;
	}
	
	private String getNewUrl(HttpServletRequest request) {
		// 查看是否为.html结尾，是就转换为所需要的路径，然后放行
				String serverName = request.getServerName();
//				System.out.println("收到访问请求,访问serverName为" + serverName);
				String requestURI = request.getRequestURI();
//				System.out.println("收到访问请求,访问requestURI为" + requestURI);
				int length = requestURI.length();
				String newUrl = "/";
				if (".html".equals(requestURI.substring(length - 5, length))) {
					newUrl = requestURI.substring(1, length - 5);
//					System.out.println("截取后的newUrl" + newUrl);
					switch (checkUrl(newUrl)) {
					case 1:
						newUrl=new String(newUrl);
//						System.out.println("情况1"+newUrl);
						break;
					case 2:
						newUrl=new String("/system/index/"+newUrl);
//						System.out.println("情况2"+newUrl);
						break;
					case 3:
						newUrl=new String("/system/"+newUrl);
//						System.out.println("情况3"+newUrl);
						break;
					}	
				}
		return newUrl;
	}
	@RequestMapping("/**.html")
	public String changeUrl(HttpServletRequest request) {
		String newUrl = getNewUrl(request);
		return newUrl; 
	}
}

